package com.dmwork.service.interaction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.interaction.entity.WorkComment;
import com.dmwork.service.interaction.entity.vo.CommentMessageVo;
import com.dmwork.service.interaction.entity.vo.WorkCommentMessageVo;
import com.dmwork.service.interaction.entity.vo.WorkCommentVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.mapper.WorkCommentMapper;
import com.dmwork.service.interaction.service.WorkCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作品评论表 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-04-25
 */
@Service
public class WorkCommentServiceImpl extends ServiceImpl<WorkCommentMapper, WorkComment> implements WorkCommentService {

    @Autowired
    private CustomerFeignService customerFeignService;

    @Override
    public Page<WorkComment> getCommentPage(String workId, Integer sortType, Integer page, Integer limit) {
        QueryWrapper<WorkComment> queryWrapper = new QueryWrapper<>();
        Page<WorkComment> selectPage = new Page<>(page, limit);
        queryWrapper.eq("work_id", workId);
        queryWrapper.isNull("parent_comment_id"); //若parent_comment_id不为空的评论记录是其他评论的回复，parent_comment_id为空才是直接对作的评论
        if (sortType == 0) { //如果sortType==0，则按照热度排序
            queryWrapper.orderByDesc("like_count");
        } else if (sortType == 1) { //如果sortType==1，则按照最新时间排序
            queryWrapper.orderByDesc("gmt_create");
        }
        selectPage = baseMapper.selectPage(selectPage, queryWrapper);
        return selectPage;
    }

    @Override
    public Page<WorkComment> getReplyPage(String commentId, Integer sortType, Integer page, Integer limit) {
        QueryWrapper<WorkComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_comment_id", commentId);
        if (sortType == 0) { //如果sortType==0，则按照热度排序
            queryWrapper.orderByDesc("like_count");
        } else if (sortType == 1) { //如果sortType==1，则按照最新时间排序
            queryWrapper.orderByDesc("gmt_create");
        }
        Page<WorkComment> selectPage = new Page<>(page, limit);
        selectPage = baseMapper.selectPage(selectPage, queryWrapper);
        return selectPage;
    }

    @Override
    public List<WorkCommentMessageVo> getReplyMessageList(List<String> commentIdList) {
        List<WorkCommentMessageVo> workCommentMessageVoList = new ArrayList<>();
        //先查出所有回复的数据
        List<WorkComment> workCommentList = baseMapper.selectList(new QueryWrapper<WorkComment>().in("id", commentIdList).orderByDesc("gmt_create"));
        //遍历获取这些回复所回复的评论id，并查出这些被回复的评论
        List<String> repliedCommentIdList=new ArrayList<>();
        for (WorkComment workComment : workCommentList) {
            repliedCommentIdList.add(workComment.getRepliedCommentId());
        }
        List<WorkComment> repliedCommentList = baseMapper.selectBatchIds(repliedCommentIdList);
        //对回复消息的数据进行组装
        for (WorkComment workComment : workCommentList) {
            WorkCommentMessageVo workCommentMessageVo = new WorkCommentMessageVo();
            workCommentMessageVo.setCustomerId(workComment.getCustomerId());
            workCommentMessageVo.setCommentId(workComment.getId());
            workCommentMessageVo.setComment(workComment.getContent());
            workCommentMessageVo.setParentCommentId(workComment.getParentCommentId());
            workCommentMessageVo.setWorkId(workComment.getWorkId());
            workCommentMessageVo.setGmtCreate(workComment.getGmtCreate());
            for (WorkComment repliedComment : repliedCommentList) { //对被回复的评论进行遍历，找到当前回复的评论并进行数据组装
                if (repliedComment.getId().equals(workComment.getId())) {
                    workCommentMessageVo.setRepliedCommentId(repliedComment.getId());
                    workCommentMessageVo.setRepliedComment(repliedComment.getContent());
                    break;
                }
            }
            workCommentMessageVoList.add(workCommentMessageVo);
        }
        return workCommentMessageVoList;
    }

    @Override
    public void decreaseLikeCount(String commentId) {
        baseMapper.decreaseLikeCount(commentId);
    }

    @Override
    public void increaseLikeCount(String commentId) {
        baseMapper.increaseLikeCount(commentId);
    }

    @Override
    public List<CommentMessageVo> getCommentMessageList(List<String> workCommentIdList) {
        //查询评论表中数据
        List<WorkComment> workCommentList = baseMapper.selectBatchIds(workCommentIdList);
        //查询所有评论用户的用户名和头像
        List<String> customerIdList=new ArrayList<>(); //用户id列表
        List<String> repliedCommentIdList=new ArrayList<>(); //被回复的评论id列表
        for (WorkComment workComment : workCommentList) {
            customerIdList.add(workComment.getCustomerId());
            repliedCommentIdList.add(workComment.getRepliedCommentId());
        }
        R customerBriefInfoListResult = customerFeignService.getBriefInfoList(customerIdList);
        if (!customerBriefInfoListResult.getSuccess()) {
            throw new DmworkException(ResultCodeEnum.SELECT_CUSTOMER_INFO_ERROR);
        }
        //查询被回复的评论数据
        List<WorkComment> repliedWorkCommentList = baseMapper.selectBatchIds(repliedCommentIdList);
        Map<String, List<String>> customerInfoMap = (Map<String, List<String>>) customerBriefInfoListResult.getData().get("result");
        //数据组装
        List<CommentMessageVo> commentMessageVoList=new ArrayList<>();
        for (WorkComment workComment : workCommentList) {
            CommentMessageVo commentMessageVo = new CommentMessageVo();
            BeanUtils.copyProperties(workComment, commentMessageVo);
            commentMessageVo.setCommentId(workComment.getId());
            commentMessageVo.setComment(workComment.getContent());
            commentMessageVo.setType(0);
            commentMessageVo.setCustomerName(customerInfoMap.get(workComment.getCustomerId()).get(0));
            commentMessageVo.setCustomerAvatar(customerInfoMap.get(workComment.getCustomerId()).get(1));
            for (WorkComment repliedComment : repliedWorkCommentList) {
                if (repliedComment.getId().equals(workComment.getRepliedCommentId())) {
                    commentMessageVo.setRepliedComment(repliedComment.getContent());
                    break;
                }
            }
            commentMessageVoList.add(commentMessageVo);
        }
        return commentMessageVoList;
    }
}